package uk.co.douglashoskins.weathercompare.providers;

import java.io.IOException;

import javax.microedition.lcdui.Image;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import uk.co.douglashoskins.weathercompare.Assets;
import uk.co.douglashoskins.weathercompare.WeatherCompare;
import uk.co.douglashoskins.weathercompare.controller.WeatherObj;
import uk.co.douglashoskins.weathercompare.services.NetQueueCallback;
import uk.co.douglashoskins.weathercompare.util.Weather;

public class WUnderground implements NetQueueCallback, WeatherProvider {
	private int state;
	private WeatherCallback wc;
	public void getWeather(double lat, double lng, WeatherCallback wc) {
		this.wc = wc;
		String yql = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%20%3D%20%22http%3A%2F%2Fapi.wunderground.com%2Fauto%2Fwui%2Fgeo%2FGeoLookupXML%2Findex.xml%3Fquery%3D" + lat + "%2C" + lng + "%22&format=json";
		state = 0;
		WeatherCompare.n.addRequest(yql, this);
	}

	private void getCode(JSONObject j) throws JSONException {
		String code = j.getJSONObject("location").getJSONObject("nearby_weather_stations").getJSONObject("airport").getJSONArray("station").getJSONObject(0).getString("icao");
		
		String yql = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%20%3D%20%22http%3A%2F%2Fapi.wunderground.com%2Fauto%2Fwui%2Fgeo%2FForecastXML%2Findex.xml%3Fquery%3D" + code + "%22&format=json";
		state = 1;
		WeatherCompare.n.addRequest(yql, this);
	}
	
	private String[] snow = {"flurries", "sleat", "sleet", "snow"};
	private String[] rain = {"rain"};
	private String[] storm = {"storm"};
	private String[] sun = {"sunny", "clear"};
	
	private int getCode(String icon) {
		icon = icon.toLowerCase();
		for (int i=0; i<snow.length; i++) {
			if (icon.indexOf(snow[i]) != -1) {
				return Weather.SNOW;
			}
		}
		
		for (int i=0; i<rain.length; i++) {
			if (icon.indexOf(rain[i]) != -1) {
				return Weather.RAIN;
			}
		}
		
		for (int i=0; i<storm.length; i++) {
			if (icon.indexOf(storm[i]) != -1) {
				return Weather.THUNDERSTORM;
			}
		}
		
		for (int i=0; i<sun.length; i++) {
			if (icon.indexOf(sun[i]) != -1) {
				return Weather.SUN;
			}
		}
		
		return Weather.CLOUD;
	}
	
	private void getWeather(JSONObject j) throws JSONException {
		JSONArray forecast = j.getJSONObject("forecast").getJSONObject("simpleforecast").getJSONArray("forecastday");
		JSONObject today = forecast.getJSONObject(0);
		JSONObject tomorrow = forecast.getJSONObject(1);
		WeatherObj wo = new WeatherObj();
		wo.today.text = today.getString("conditions");
		wo.today.high = Integer.parseInt(today.getJSONObject("high").getString("celsius"));
		wo.today.low = Integer.parseInt(today.getJSONObject("low").getString("celsius"));
		wo.today.code = getCode(today.getString("icon"));
		wo.tomorrow.text = tomorrow.getString("conditions");
		wo.tomorrow.high = Integer.parseInt(tomorrow.getJSONObject("high").getString("celsius"));
		wo.tomorrow.low = Integer.parseInt(tomorrow.getJSONObject("low").getString("celsius"));
		wo.tomorrow.code = getCode(tomorrow.getString("icon"));
		wc.weatherCB(wo, this);
	}
	
	public void netCallback(String s) throws IOException {
		try {
			JSONObject j = new JSONObject(s);
			JSONObject results = j.getJSONObject("query").getJSONObject("results");
			
			switch (state) {
				case 0: {
					getCode(results);
					break;
				}
				
				case 1: {
					getWeather(results);
					break;
				}
			}
			j = null;
			System.gc();
		} catch (JSONException e) {
			wc.weatherErr(this);
		}
	}

	public void netException(Exception exc) {
		wc.weatherErr(this);
	}

	public Image logo() { return Assets.WUNDERGROUND; }

	public String name() { return "Weather Underground"; }
}
